/**
 * @Title: model.js
 * @Description: model
 * @author: Fanchao
 * @date: 2020/8/25 1:18 上午
 * @version V1.0
 */

import { pathToRegexp } from "path-to-regexp";
import { message } from "antd";
import {
    createEvaluateExam,
    createEvaluatePlan,
    createEvaluateQuestion,
    deleteEvaluateQuestion,
    getEvaluatePlanModelList,
    getQuestionCategoryList,
    getQuestionModelList,
    updateEvaluateQuestion
} from "api/index";

export default {
    namespace: "questions",
    state: {
        evaluationDrawerVisible: false,
        evaluateQuestionDrawerVisible: false,
        evaluationPlanModalVisible: false,
        title: "新增评价等级",
        evaluatePlanList: [],
        itemData: {},
        evaluatePlanIdOption: null,
        evaluateQuestionTypeOption: null,
        questionList: [],
        selectedRowKeys: [],
        evaluatePlanId: "",
        evaluateQuestionType: "",
        questionCategoryList: []
    },
    reducers: {
        setEvaluationDrawerVisible(state, { payload: evaluationDrawerVisible }) {
            return { ...state, evaluationDrawerVisible };
        },
        setEvaluateQuestionDrawerVisible(state, { payload: evaluateQuestionDrawerVisible }) {
            return { ...state, evaluateQuestionDrawerVisible };
        },
        setEvaluationPlanModal(state, { payload: evaluationPlanModalVisible }) {
            return { ...state, evaluationPlanModalVisible };
        },
        setTitle(state, { payload: title }) {
            return { ...state, title };
        },
        setEvaluatePlanList(state, { payload: evaluatePlanList }) {
            return { ...state, evaluatePlanList };
        },
        setItemData(state, { payload: itemData }) {
            return { ...state, itemData };
        },
        setEvaluatePlanIdOption(state, { payload: evaluatePlanIdOption }) {
            return { ...state, evaluatePlanIdOption };
        },
        setEvaluateQuestionTypeOption(state, { payload: evaluateQuestionTypeOption }) {
            return { ...state, evaluateQuestionTypeOption };
        },
        setQuestionList(state, { payload: { questionList, evaluatePlanId, evaluateQuestionType } }) {
            return { ...state, questionList, evaluatePlanId, evaluateQuestionType };
        },
        setSelectedRowKeys(state, { payload: selectedRowKeys }) {
            return { ...state, selectedRowKeys };
        },
        setQuestionCategoryList(state, { payload: questionCategoryList }) {
            return { ...state, questionCategoryList };
        }
    },
    effects: {
        /**
         * @desc 创建评价等级
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* addEvaluatePlan({ payload, callback }, { call, put, select }) {
            const result = yield call(createEvaluatePlan, payload);
            if (result.status === "success") {
                message.success(`添加成功`);
                if (callback && typeof callback === "function") {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 评价等级列表
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getEvaluatePlanList({ payload, callback }, { call, put, select }) {
            const result = yield call(getEvaluatePlanModelList, payload);
            if (result.status === "success") {
                const evaluatePlanList = result.result;
                yield put({
                    type: "setEvaluatePlanList",
                    payload: evaluatePlanList
                });
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 创建方案创建题目
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* addEvaluateQuestion({ payload, callback }, { call, put, select }) {
            const result = yield call(createEvaluateQuestion, payload);
            if (result.status === "success") {
                message.success(`添加${payload.category}题目成功`);
                if (callback && typeof callback === "function") {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 更新评价题目
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* editEvaluateQuestion({ payload, callback }, { call, put, select }) {
            const result = yield call(updateEvaluateQuestion, payload);
            if (result.status === "success") {
                message.success(`修改${payload.category}题目成功`);
                if (callback && typeof callback === "function") {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 获取方案创建列表
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getQuestionList({ payload, callback }, { call, put, select }) {
            const result = yield call(getQuestionModelList, payload);
            if (result.status === "success") {
                const list = result.result;
                const questionList = list.map(item => {
                    return {
                        category: item.category,
                        checkPoint: item.checkPoint,
                        evaluatePlanId: item.evaluatePlanId,
                        evaluatePlanName: item.evaluatePlanName,
                        evaluateQuestionId: item.evaluateQuestionId,
                        weight: item.weight,
                        key: item.evaluateQuestionId
                    };
                });
                yield put({
                    type: "setQuestionList",
                    payload: {
                        questionList,
                        evaluatePlanId: payload.evaluatePlanId,
                        evaluateQuestionType: payload.questionCategoryName
                    }
                });
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 创建试卷
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* addEvaluateExam({ payload, callback }, { call, put, select }) {
            const result = yield call(createEvaluateExam, payload);
            if (result.status === "success") {
                if (callback && typeof callback === "function") {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 评教类型
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getQuestionCategoryList({ payload }, { call, put, select }) {
            const result = yield call(getQuestionCategoryList, payload);
            if (result.status === "success") {
                const questionCategoryList = result.result;
                yield put({
                    type: "setQuestionCategoryList",
                    payload: questionCategoryList
                });
            } else {
                message.error(result.message);
            }

        },
        * deleteEvaluateQuestion({ payload, callback }, { call, put, select }) {
            const result = yield call(deleteEvaluateQuestion, payload);
            if (result.status === "success") {
                message.success(`删除成功`);
                if (callback && typeof callback === "function") {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname }) => {
                if (pathToRegexp("/evaluations/question").exec(pathname)) {
                }
            });
        }
    }
};
